package com.example.printscreen.printscreen.bean;

import android.content.SharedPreferences;

import com.example.printscreen.printscreen.utils.Constant;
import com.example.printscreen.printscreen.utils.SharedPreferenceUtil;

/**
 *  当前登录用户
 */
public class LoginUser {
    private String code;
    private String name;
    private String begTime;
    private String endTime;
    private String roundTime;
    private String url;

    private static LoginUser instance = null;

    public static synchronized LoginUser getInstance() {
        if (instance == null) {
            instance = getLocalUserData();
        }
        return instance;
    }

    /**
     *  从sp中读取数据,返回LoginUser实例
     */
    private static LoginUser getLocalUserData() {
        LoginUser user = new LoginUser();
        SharedPreferences sp = SharedPreferenceUtil.getSp();

        user.code = sp.getString(Constant.SPKey.CODE, "");
        user.name = sp.getString(Constant.SPKey.NAME, "");
        user.begTime = sp.getString(Constant.SPKey.BEGIN_TIME, "");
        user.endTime = sp.getString(Constant.SPKey.END_TIME, "");
        user.roundTime = sp.getString(Constant.SPKey.ROUND_TIME, "");
        user.url = sp.getString(Constant.SPKey.URL, "");
        return user;
    }

    /**
     *  从接口返回数据获取登录用户数据
     */
    public void initFromResponse(LoginInfo data) {
        code = data.getCode();
        name = data.getName();
        begTime = data.getBeg_time();
        endTime = data.getEnd_time();
        roundTime = data.getRound();
        url = data.getUrl();

        savaDataSP();
    }

    /**
     *  保存用户数据到SP
     */
    private void savaDataSP() {
        SharedPreferences.Editor editor = SharedPreferenceUtil.getEditor();

        editor.putString(Constant.SPKey.CODE, code);
        editor.putString(Constant.SPKey.NAME, name);
        editor.putString(Constant.SPKey.BEGIN_TIME, begTime);
        editor.putString(Constant.SPKey.END_TIME, endTime);
        editor.putString(Constant.SPKey.ROUND_TIME, roundTime);
        editor.putString(Constant.SPKey.URL, url);
        editor.apply();
    }

    /**
     * 存取token相关
     */
    public static final class TokenVerity {
        /**
         * 设置token
         */
        public static void setToken(String token) {
            SharedPreferences.Editor editor = SharedPreferenceUtil.getEditor();
            editor.putString(Constant.SPKey.TOKEN, token);
            editor.commit();
        }

        /**
         * 获取token
         */
        public static String getToken() {
            String token = SharedPreferenceUtil.getSp().getString(Constant.SPKey.TOKEN, "");
            return token;
        }
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBegTime() {
        return begTime;
    }

    public void setBegTime(String begTime) {
        this.begTime = begTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getRoundTime() {
        return roundTime;
    }

    public void setRoundTime(String roundTime) {
        this.roundTime = roundTime;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
